package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.User;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * This class represents a user id type.
 * 
 * @author Yudi Xue
 * 
 */
public class UserID extends BaseId {

	public UserID(UUID id) throws SilverBusinessLogicErrorException {
		super(id);
	}

	public String toString() {
		return "user" + uIdVal.toString();
	}

	public UUID getUUIdVal() {
		return uIdVal;
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((UserID) id)
				.getUUIdVal().compareTo(this.getUUIdVal()) == 0));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		String name = borrowedObj.getFirstName(this, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		GroupID[] gIds = borrowedObj.getParticipatedGroupIds(this, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return gIds;
	}

	@Override
	public boolean isEntity() {
		return true;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		// user is the top level of the hierarchy and does not have parent
		return null;
	}

	@Override
	public boolean hasChildren() {
		return true;
	}

	public void accept(SilverIDVisitor v) throws Exception {
		v.visit(this);
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		String name = borrowedObj.getLastName(this, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	/**
	 * Update First Name
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.setFirstName(this, updateName, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	/**
	 * User do not have a simulation type
	 * 
	 * @return An empty String
	 */
	@Override
	public String getTypeFromDatabase(StorageI stoarge) throws Exception {
		return "";
	}

	/**
	 * User do not have a description
	 * 
	 * @return An empty String
	 */
	@Override
	public String getDescriptionFromDatabase(StorageI stoarge) throws Exception {
		return "";
	}

	/**
	 * User do not have a description
	 * 
	 * @return false
	 */
	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		return false;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		Date result = borrowedObj.getRegistrationDate(this, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		User borrowedObj = ((User) User.getObjectPoolFactory().borrowObject());
		boolean result = borrowedObj.deleteUser(this, storage);
		User.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}
}
